package com.china08.yxyapi.service.lesson.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.index.query.TermsQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;

import com.china08.yxyapi.entity.es.ElasticYxyYxyQuesResource;
import com.china08.yxyapi.service.lesson.TecCloudQuesLessonService;

@Service
public class TecCloudQuesLessonServiceImpl implements TecCloudQuesLessonService {

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    @Override
    public Page<ElasticYxyYxyQuesResource> questionFindByKnowAndLevelAndType(String know, String level, String type,
            String userId, String subject, Integer status, Pageable pageable) {
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        if (StringUtils.isNotBlank(know) && !StringUtils.equals("all", know) && status == 1) {
            TermQueryBuilder builder = new TermQueryBuilder("knowFullIds", Integer.parseInt(know));
            boolQueryBuilder.must(builder);
        } else {
            TermQueryBuilder builder = new TermQueryBuilder("subject", subject);
            boolQueryBuilder.must(builder);
        }
        // 基本查询
        boolQueryBuilder = get(boolQueryBuilder, level, type);
        Page<ElasticYxyYxyQuesResource> page = getPage(boolQueryBuilder, pageable);
        return page;
    }

    @Override
    public BoolQueryBuilder get(BoolQueryBuilder boolQueryBuilder, String level, String type) {
        // 只查询未删除的
        TermQueryBuilder deleteBuilder = new TermQueryBuilder("deleteFlag", "0");
        boolQueryBuilder.must(deleteBuilder);

        // 只能查询不是私人的
        TermQueryBuilder viewScopeBuilder = new TermQueryBuilder("viewScope", 0);
        boolQueryBuilder.mustNot(viewScopeBuilder);
        //
        if (StringUtils.isNotBlank(level) && !StringUtils.equals("all", level)) {
            TermQueryBuilder builder = new TermQueryBuilder("difficultyLevel", level);
            boolQueryBuilder.must(builder);
        }
        if (StringUtils.isNotBlank(type) && !StringUtils.equals("all", type)) {
            if (StringUtils.equals("6", type)) {
                List<String> notquesType = new ArrayList<String>();
                notquesType.add("1");
                notquesType.add("2");
                notquesType.add("8");
                TermsQueryBuilder builder = new TermsQueryBuilder("questionType", notquesType);
                boolQueryBuilder.mustNot(builder);
            } else {
                TermQueryBuilder builder = new TermQueryBuilder("questionType", type);
                boolQueryBuilder.must(builder);
            }
        } else {
            //
            // 目前只有 单选 和多选
            // BoolQueryBuilder typeOr = new BoolQueryBuilder();
            // TermQueryBuilder builder1 = new TermQueryBuilder("questionType",
            // "1");
            // TermQueryBuilder builder8 = new TermQueryBuilder("questionType",
            // "8");
            // typeOr.should(builder1);
            // typeOr.should(builder8);
            // boolQueryBuilder.must(typeOr);
        }

        return boolQueryBuilder;
    }

    /**
     * 查询page
     * 
     * @param boolQueryBuilder
     * @param pageable
     * @return
     */
    private Page<ElasticYxyYxyQuesResource> getPage(BoolQueryBuilder boolQueryBuilder, Pageable pageable) {
        SearchQuery query = new NativeSearchQueryBuilder().withPageable(pageable).withQuery(boolQueryBuilder).build();
        Page<ElasticYxyYxyQuesResource> page = elasticsearchTemplate.queryForPage(query,
                ElasticYxyYxyQuesResource.class);
        return page;
    }
}
